<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined"></Icon>
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <a-button>批量操作
          <Icon icon="mdi:chevron-down"></Icon>
        </a-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <FileRecordDesc @register="registerDesc"></FileRecordDesc>
</template>
<script lang="ts" name="FileRecord" setup>
import { ref } from 'vue'
import { downloadByOnlineUrl } from '/@/utils/file/download';
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import { useMessage } from '/@/hooks/web/useMessage';
import FileRecordDesc from './DetailDrawer.vue';
import { columns, searchFormSchema } from './fileRecord.data';
import { list, deleteFileRecord, batchDeleteFileRecord } from './fileRecord.api';
import { useListPage } from '/@/hooks/system/useListPage'
import { Icon } from '/@/components/Icon'
  
const showFooter = ref(true);
const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();
const { createMessage } = useMessage();
// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'file-record',
  tableProps: {
    title: '文件记录列表',
    api: list,
    showIndexColumn: true,
    rowSelectionTipType: 'onTableTitle',
    striped: false,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema
    },
    bordered: false,
    actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
  }
})
const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 详情
 */
function handleDetail(record) {
  showFooter.value = false;
  openRoleDesc(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteFileRecord({ id: record.id }, reload);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteFileRecord({ ids: selectedRowKeys.value }, reload);
}

const handleDownload = (record) => {
  downloadByOnlineUrl(record.fileUrl, record.originalName)
}

/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    },
    {
      label: '下载',
      onClick: handleDownload.bind(null, record),
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    }
  ]
}

</script>
